import {
    validatorUnitNameByAdd,
    validatorCreditNumberByAdd,
    addUint
} from "@/views/authorization-management/apply-unit/api/apply-unit.js";
import dbl from "@/utils/dbl";
import { getDict } from "~/utils";

export default {
    data() {
        /**
         * async / await 同步
         * 校验申请单位的唯一性
         */
        let validatorUnitName = async (rule, value, callback) => {
            if (value == "") {
                callback(new Error("请输入单位名称"));
            }
            await validatorUnitNameByAdd(value).then(respData => {
                let result = respData.data.data;
                if (result == false) {
                    callback(new Error("单位名称重复"));
                }
            });
        };
        let validatorPhone = async (rule, value, callback) => {
            if (value == "") {
                callback(new Error("请输入联系人手机"));
            }
            const reg = /^1[0-9]{10}$/;
            if (!reg.test(value)) {
                callback(new Error("请输入合法的联系人手机"));
            }
        };
        /**
         * async / await 同步
         * 校验社会统一信用码唯一性
         */
        let validatorCreditNumber = async (rule, value, callback) => {
            if (value == "") {
                callback(new Error("请输入社会统一信用码"));
            }
            await validatorCreditNumberByAdd(value).then(respData => {
                let result = respData.data.data;
                if (result == false) {
                    callback(new Error("社会统一信用码重复"));
                }
            });
        };
        return {
            //上次文件相关
            busId: "",

            myHeader: { Authorization: dbl.get("token") },
            //弹出框 初始时隐藏
            dialogVisible: false,
            formObj: {
                applyUnit: {
                    apUiId: "",
                    unitName: "",
                    unitType: "",
                    userName: "",
                    tell: "",
                    creditNumber: "",
                    sort: "",
                    applyProperty: "",
                    remarks: ""
                },
                //附件
                fileList: [],
                url: "/api/applyunit/upload"
            },
            lists: [],
            rules: {
                applyUnit: {
                    unitName: [
                        {
                            required: true,
                            message: "请输入单位名称",
                            trigger: "blur"
                        },
                        { validator: validatorUnitName, trigger: "blur" },
                        { min: 2, max: 50, message: "单位名称长度必须小于50" }
                    ],
                    unitType: [
                        {
                            required: true,
                            message: "请选择单位类型",
                            trigger: "change"
                        }
                    ],
                    userName: [
                        {
                            required: true,
                            message: "请输入联系人姓名",
                            trigger: "blur"
                        },
                        { min: 2, max: 20, message: "联系人姓名必须小于20" }
                    ],
                    tell: [
                        {
                            required: true,
                            message: "请输入联系电话",
                            trigger: "blur"
                        },
                        { validator: validatorPhone, trigger: "blur" }
                    ],
                    creditNumber: [
                        {
                            required: true,
                            message: "请输入社会统一信用码",
                            trigger: "blur"
                        },
                        {
                            min: 18,
                            max: 18,
                            message: "社会统一信用代码长度为18位"
                        },
                        { validator: validatorCreditNumber, trigger: "blur" }
                    ],
                    sort: [
                        {
                            required: true,
                            message: "请输入排序号",
                            trigger: "blur"
                        }
                    ],
                    applyProperty: [
                        {
                            required: true,
                            message: "请输入物业",
                            trigger: "blur"
                        }
                    ]
                }
            }
        };
    },
    mounted() {},
    created() {
        this.lists = getDict("apply-unit-type");
    },
    methods: {
        /**
         * 展示新增弹框
         */
        show() {
            //初始化数据 清除上次添加遗留的数据
            this.formObj.applyUnit.apUiId = "";
            this.formObj.applyUnit.unitName = "";
            this.formObj.applyUnit.unitType = "";
            this.formObj.applyUnit.userName = "";
            this.formObj.applyUnit.tell = "";
            this.formObj.applyUnit.creditNumber = "";
            this.formObj.applyUnit.sort = "";
            this.formObj.applyUnit.applyProperty = "";
            this.formObj.applyUnit.remarks = "";
            this.formObj.fileList = [];

            this.loading = false;
            this.dialogVisible = true;
        },

        /**
         * 关闭弹窗
         * @param done
         */
        beforeClose(done) {
            this.dialogVisible = false;
            done();
        },

        // 上传成功
        sendData() {
            this.$refs.ruleForm.validate(async valid => {
                if (valid) {
                    // 不管有没有上传附件，都会触发onSuccess的钩子，所以提交表单的事件不需要填两遍了
                    this.$refs.unitFileUpload.submit(
                        "apply-unit",
                        this.formObj.applyUnit.apUiId
                    );
                }
            });
        },
        // 表单提交事件
        async saveEntity(response, file, fileList) {
            this.formObj.applyUnit.apUiId = response.data;
            let params = { ...this.formObj };
            const res = await addUint(params);
            if (res.data.code == 0) {
                this.$message.success(`操作成功!`);
                this.dialogVisible = false;
                this.$parent.query();
            } else {
                this.dialogVisible = false;
                this.$message.error("操作失败");
            }
        },
        /**
         * 上传附件相关
         * @param file
         * @param fileList
         */
        handlePreview(file) {
            console.log(file);
        },
        handleExceed(files, fileList) {
            this.$message.warning(
                `当前限制选择 3 个文件，本次选择了 ${
                    files.length
                } 个文件，共选择了 ${files.length + fileList.length} 个文件`
            );
        },
        beforeRemove(file, fileList) {
            return this.$confirm(`确定移除 ${file.name}？`);
        },

        /**
         * 删除文件
         */
        handleRemove(file, fileList) {
            this.editObj.fileList = fileList;
            //console.log(this.editObj.fileList);
        },

        /**
         * 上传完成给fileList赋值
         */
        uploadSuccess(response, file, fileList) {
            //拿到文件名
            let fileName = file.name;
            //拿到上传的文件地址
            let filePath = file.response.data[0].path;

            let files = { name: "", path: "" };
            files.name = fileName;
            files.path = filePath;
            this.formObj.fileList.push(files);
        },

        /**
         * 重置
         */
        resetForm() {
            this.$refs["ruleForm"].resetFields();
        }
    }
};
